// pages/mes/issue-preview/issue-preview.js
const app = getApp();
import utils from '../../../utils/util.js';
import Toast from '../../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isActive: false,
        planTxt: "1、和服务者(乙方)确定入职目标公司和服务细节后，候选人(甲方)需将服务费冻结托管至闲将。\n2、成功入职15天后，托管的服务费将支付至乙方账户中。\n3、未能入职或者入职15天内离职(无论被解雇或主动离职) ，甲方都可提出并获得全额退款。\n",
        typeName: '',
        childList: [],
        currentDate: utils.formatDate(new Date()),
        contentObj: {},
        serviceContent:''
    },
    lookClick() {
        this.setData({
            isActive: !this.data.isActive
        })
    },
    issueClick() {
        // if (app.userInfo.IsVerified == 0 || app.userInfo.IsVerified == 1) {
        //     Toast("您还未实名认证，请先实名认证！");
        //     return;
        // }
        let that = this;
        that.checkLabel().then(res => {
            if(res){
                let relations = [];
                let createDateTime = utils.formatTime(new Date());
                let CreateDateTimeText = utils.formatDate(new Date());
                wx.showToast({
                    title: '提交中...',
                    icon: 'loading',
                    mask: true
                });
                let parmas = JSON.stringify(that.data.childList);
                that.data.childList.forEach(function (item) {
                    // console.log(JSON.stringify(item));
                    let obj = {};
                    obj.CreateDateTime = createDateTime;
                    obj.ServiceId = '';
                    obj.ChildServiceTypeName = item.typeName;
                    obj.ChildServiceType = item.Id;
                    relations.push(obj);
                });
                // console.log(JSON.stringify(relations));
                let token = "Bearer " + app.globalData.token;
                let data = {
                    Id: '',
                    UserInfoId: app.userInfo.Id,
                    ParentServiceType: that.data.contentObj.parentId,
                    ParentServiceTypeName: that.data.contentObj.parentTypeName,
                    Relations: relations,
                    ServiceTitle: that.data.contentObj.title,
                    ServiceContent: that.data.serviceContent,
                    Price: that.data.contentObj.amount,
                    Unit: that.data.contentObj.unit,
                    UnitDes: that.data.contentObj.unitDes.UnitDes,
                    Status: 0,
                    CreateDateTime: createDateTime,
                    CreateDateTimeText: CreateDateTimeText
                };
                console.log(JSON.stringify(data));
                wx.request({
                    url: app.globalData.serverAddress.addIssueService,
                    data: data,
                    method: 'POST',
                    header: {
                        'Authorization': token
                    },
                    success: function (res) {
                        console.log(JSON.stringify(res));
                        if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                            Toast("上传成功！");
                            wx.navigateTo({
                                url: '/pages/order-detail/order-detail?id=' + res.data.BodyContent.Id + "&price=" + that.data.contentObj.amount,
                            })
                        } else {
                            Toast(res.data.Message);
                        }
                    },
                    fail: function (res) {
                        console.log(JSON.stringify(res));
                    },
                    complete: function (res) {
                        wx.hideToast();
                    }
                })
            }else{
                Toast("请先前往个人中心设置头衔/机构");
                setTimeout(function(){
                    wx.navigateTo({
                        url: '/pages/mes/edit-home/edit-home'
                    })
                },500)
            }
        },err => {
            console.log(err);
        });
    },
    checkLabel(){
        let promis = new Promise(function(resole , reject){
            wx.showLoading({
                title: '加载中...',
                mask: true
            })
            wx.request({
                url: app.globalData.serverAddress.checkUserLabel,
                data: {
                    userId:app.userInfo.Id
                },
                header: {
                    'Authorization': "Bearer " + app.globalData.token
                },
                method: 'GET',
                success: function(res) {
                    // console.log(res);
                    if (res.statusCode == 200 && res.data.CodeStatus == 20000){
                        resole(res.data.BodyContent);
                    }else{
                        reject(res);
                    }
                },
                fail: function(res) {reject(res)},
                complete:function(res){wx.hideLoading();}
            })
        });
        return promis;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        let obj = JSON.parse(options.obj);
        this.setData({
            contentObj: obj,
            childList: obj.typeData,
            serviceContent: app.globalData.serviceContent
            // typeName: obj.parentTypeName,
            // price: obj.amount,
            // unitAmount: obj.unit,
            // childList: obj.typeData,
            // title: obj.title,
            // content: obj.content,
            // parentId: obj.parentId,
            // unitDes: obj.unitDes.UnitDes,
            // nickName: obj.nickName,
            // avatarUrl: obj.avatarUrl
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})